package com.ipet.app.ui.common;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.bm.library.PhotoView;
import com.ipet.app.R;
import com.ipet.app.ui.base.SwipeBackActivity;
import com.ipet.app.util.ImageUtil;

/**
 * Created by jindongping on 16/3/22.
 */
public class PhotoViewActivity extends SwipeBackActivity {

    private String[] images;
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_view);
        images = getIntent().getStringArrayExtra("images");
        position = getIntent().getIntExtra("position", 0);
        if (images == null || images.length == 0) {
            finish();
            return;
        }
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setPageMargin((int) (getResources().getDisplayMetrics().density * 15));
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return images.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                PhotoView view = new PhotoView(container.getContext());
                view.setAdjustViewBounds(true);
                view.enable();
                ImageUtil.getInstance(container.getContext()).disPaly("m_" + images[position], view);
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });
        viewPager.setCurrentItem(position);
    }
}
